package com.blacktry.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blacktry.entity.model.ApiResult;
import com.blacktry.entity.easypoidata.DeriveExcel;
import com.blacktry.entity.model.ExamManage;
import com.blacktry.mapper.DeriveExcelMapper;
import com.blacktry.service.DeriveExcelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blacktry.utils.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author blackTry
 * @since 2021-06-23
 */
@Service
public class  DeriveExcelServiceImpl extends ServiceImpl<DeriveExcelMapper, DeriveExcel> implements DeriveExcelService {

    @Override
    public Integer insert(DeriveExcel deriveExcel) {
        return this.baseMapper.insert(deriveExcel);
    }

    @Override
    public List<DeriveExcel> select(Integer id) {
        LambdaQueryWrapper<DeriveExcel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeriveExcel::getExamId,id);
        // todo 查出来直接赋值url
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public List<DeriveExcel> selectListByIds(String[] ids) {
        List<String> list = Arrays.asList(ids);
        return this.baseMapper.selectBatchIds(list);
    }

    @Override
    public List<DeriveExcel> select() {
        return this.baseMapper.selectList(null);
    }

    @Override
    public Page<DeriveExcel> selectPage(DeriveExcel deriveExcel, Page<DeriveExcel> deriveExcelPage) {
        LambdaQueryWrapper<DeriveExcel> wrapper = new LambdaQueryWrapper<>();
        // 根据科目，班级，年级，描述，禁用条件查询

        if(deriveExcel.getUsername()!=null){
            wrapper.eq(DeriveExcel::getUsername,deriveExcel.getUsername());
        }
        if(deriveExcel.getName()!=null){
            wrapper.eq(DeriveExcel::getName,deriveExcel.getName());
        }
        if(deriveExcel.getAffiliation()!=null){
            wrapper.like(DeriveExcel::getAffiliation,deriveExcel.getAffiliation());
        }
        if(deriveExcel.getExamId()!=null){
            wrapper.eq(DeriveExcel::getExamId,deriveExcel.getExamId());
        }
        // 当前的时激活状态的学生试卷
        wrapper.eq(DeriveExcel::getEnable,1);


        return this.baseMapper.selectPage(deriveExcelPage,wrapper);
    }
    @Override
    public DeriveExcel selectOne(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public Integer deleteBatchIds(String[] ids) {
        List<String> idsList = Arrays.asList(ids);
        return this.baseMapper.deleteBatchIds(idsList);
    }


    @Override
    public Integer updateOrInsert(DeriveExcel deriveExcel) {
        if(StrUtil.isBlank(String.valueOf(deriveExcel.getId()))){
            // 没有传入ID直接插入数据
            return this.baseMapper.insert(deriveExcel);
        }else{
            if(selectOne(deriveExcel.getId()) == null){
                // 导入数据的ID没有与表中的ID重复则插入数据
                return this.baseMapper.insert(deriveExcel);
            }else{
                // 传入的ID与表中已有的ID重复则更新数据
                return this.baseMapper.updateById(deriveExcel);
            }
        }

    }
   @Autowired
   DeriveExcelMapper deriveExcelMapper;
    @Override
    public Integer updateOrInsert2(DeriveExcel deriveExcel) {
        if(deriveExcelMapper.selectDeriveByExamIdAndUsername(deriveExcel.getExamId(),deriveExcel.getUsername()).size()<1){
            //没有此条数据 插入
            return this.baseMapper.insert(deriveExcel);
        }else{
            //有此条数据 更新
            return this.baseMapper.updateById(deriveExcel);
        }
    }

    @Override
    public List<DeriveExcel> selectDeriveByExamIdAndUsername(String examId, String userName) {
       return this.baseMapper.selectDeriveByExamIdAndUsername(examId,userName);
    }


}
